package edu.ustb.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ustb.domain.Address;
import edu.ustb.domain.Cart;
import edu.ustb.domain.Goods;
import edu.ustb.domain.User;
import edu.ustb.mapper.CartMapper;
import edu.ustb.service.AddressService;
import edu.ustb.service.AddressServiceImpl;
import edu.ustb.service.CartIndent;
import edu.ustb.service.CartIndentImpl;
import edu.ustb.utils.MybatisUtil;

@WebServlet("/toCartIndent.do")
public class CartIndentController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		//设置请求数据的编码格式
		request.setCharacterEncoding("utf-8");
		User user = (User) request.getSession().getAttribute("user");
		int userId=user.getUserId();
		
		//如果是从购物车界面跳转到订单界面，那么获取复选框的选取的商品id
		String []checkid1=request.getParameterValues("cartIds");
	    List<String> checkid = new ArrayList<String>();
	    for (int i=0;i<checkid1.length;i++) {
			checkid.add(checkid1[i]);
		}  

		CartIndent cartIndent=new CartIndentImpl();
		AddressService addressService = new AddressServiceImpl();
		
		List<Goods> goodsList2 =cartIndent.goodsInCartIndent(checkid);
		List<Cart> cartList2 =cartIndent.cartToIndent(checkid);
		List <Address> addressList =addressService.getAddress(userId);
		
		
		request.setAttribute("goodsList2", goodsList2);
		request.setAttribute("cartList2", cartList2);
		request.setAttribute("addressList", addressList);
		request.getRequestDispatcher("/Cart-indent.jsp").forward(request, response);
	}

}